package com.spelet.simulation;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.utils.ScissorStack;
import com.spelet.StaticFonts;
import com.spelet.StaticSprites;
import com.spelet.StaticTextures;
import com.spelet.StaticVariables;
import com.spelet.VariableCollection;

public class ChooseTerrainBrush {

	public int[] valToVarVal;
	public Sprite background;
	public Sprite border;
	public Sprite yes;
	public Sprite no;
	public float deltaX;
	public Vector2[] positions;
	public Sprite marked;
	public Rectangle rb;
	public boolean resetDrag;
	float lastDeltaStep;
	public int selectedType;
	public String[] descriptions;
	public ChooseTerrainBrush() {
		positions = new Vector2[StaticVariables.amountOfTerrainTypes];
		descriptions = new String[StaticVariables.amountOfTerrainTypes];
		valToVarVal = new int[StaticVariables.amountOfTerrainTypes];
		valToVarVal[StaticVariables.grassVal] = VariableCollection.grass;
		valToVarVal[StaticVariables.holeVal] = VariableCollection.hole;
		valToVarVal[StaticVariables.dirtVal] = VariableCollection.dirt;
		valToVarVal[StaticVariables.bluepillarVal] = VariableCollection.pillar_blue;
		valToVarVal[StaticVariables.bluebuttonVal] = VariableCollection.button_blue;
		valToVarVal[StaticVariables.yellowbuttonVal] = VariableCollection.button;
		valToVarVal[StaticVariables.yellowpillarVal] = VariableCollection.pillar_yellow;
		//Background
		background = StaticTextures.mapEdit.createSprite("background");
		border = StaticTextures.mapEdit.createSprite("border");
		marked = StaticTextures.mapEdit.createSprite("marked");
		yes = StaticTextures.mapEdit.createSprite("yes");
		no = StaticTextures.mapEdit.createSprite("no");
		marked.setPosition(180, 350);
	
		//Grass
		positions[StaticVariables.grassVal] = new Vector2(100,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.grassVal] = "Grass. Can sometimes host a flower that will attract a nearby bee.";
		//Hole
		positions[StaticVariables.holeVal] = new Vector2(180,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.holeVal] = "An endless hole. A moveblock will fill the whole and allow GreenHatMan to walk over it. Anything but a moveblock, GreenHatMan included, will fall down the endless pit and dissapear into oblivion.";

		//Dirt
		positions[StaticVariables.dirtVal] = new Vector2(260,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.dirtVal] = "Dirt. Be careful with your shoes there will ya'?";
		
		//Yellow Button
		positions[StaticVariables.yellowbuttonVal] = new Vector2(340,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.yellowbuttonVal] = "A yellow button. If all yellow buttons on the map is pressed, all the yellow pillars will be pulled into the ground!";
		//Yellow pillar
		positions[StaticVariables.yellowpillarVal] = new Vector2(420,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.yellowpillarVal] = "A yellow pillar. Will be pulled down into the ground if all yellow buttons on the map are pressed.";
		//Blue button
		positions[StaticVariables.bluebuttonVal] = new Vector2(500,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.bluebuttonVal] = "A blue button. If all blue buttons on the map is pressed, all the blue pillars will be pulled into the ground!";
		//Blue pillar
		positions[StaticVariables.bluepillarVal] = new Vector2(580,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.bluepillarVal] = "A blue pillar. Will be pulled down into the ground if all blue buttons on the map are pressed.";
		
		background.setX(Gdx.graphics.getWidth()*0.88f/2-background.getWidth()/2);
		background.setY(Gdx.graphics.getHeight()/2-background.getHeight()/2*0.47f);
		border.setX(Gdx.graphics.getWidth()*0.88f/2-border.getWidth()/2);
		border.setY(Gdx.graphics.getHeight()/2-border.getHeight()/2*0.47f);
		rb = background.getBoundingRectangle();
		for (int i = 0; i < StaticVariables.amountOfTerrainTypes; i++) {
			String[] strings = descriptions[i].split(" ");
			String finalString = "";
			String str=strings[0];
			for (int j = 1; j < strings.length; j++) {
				String s = strings[j];
				if (StaticFonts.basicFont.getBounds(str+" "+s).width > 300) {
					finalString += str+"\n";
					str = s;
				} else {
					str =str+ " " + s;
				}
			}
			finalString += str;
			descriptions[i] = finalString;
		}
	}
	public void update() {
		yes.setPosition(background.getX()+background.getWidth()-yes.getWidth()-5,background.getY()+no.getHeight()+6);
		no.setPosition(background.getX()+background.getWidth()-no.getWidth()-5,background.getY()+5);
		if (deltaX < -80-200)
			deltaX = -280;
		else if (deltaX > (StaticVariables.amountOfTerrainTypes-1)*80-200)
			deltaX=(StaticVariables.amountOfTerrainTypes-1)*80-200;
	}
	public void render(OrthographicCamera cam, SpriteBatch sb) {
		background.draw(sb);
		ScissorStack.calculateScissors(cam, sb.getTransformMatrix(), rb, rb);
		ScissorStack.pushScissors(rb);
		for (int i = 0; i < StaticVariables.amountOfTerrainTypes; i++) {
			Sprite s = StaticSprites.ctbSprites[i];
			s.setPosition(positions[i].x-deltaX, positions[i].y);
			s.draw(sb);
			if (selectedType == i) {
				marked.setPosition(positions[i].x-deltaX,positions[i].y);
				marked.draw(sb);
			}
		}
		marked.draw(sb);
		border.draw(sb);
		yes.draw(sb);
		no.draw(sb);
		sb.flush();
		ScissorStack.popScissors();
		StaticFonts.basicFont.drawMultiLine(sb, descriptions[selectedType], background.getX()+10, background.getY()+background.getHeight()*0.5f);
	}
	public boolean hit(int x, int y) {
		return (rb.contains(x,y));
	}
	public void changeDelta(int delta) {
		if (delta == 0)
			return;
		lastDeltaStep = delta;
		deltaX += delta;
		System.out.println(deltaX);
		System.out.println("DELTA" + delta);
	}
	public void pressed(float x, float y) {
		for (int i = 0; i < StaticVariables.amountOfTerrainTypes;i++) {
			if (StaticSprites.ctbSprites[i].getBoundingRectangle().contains(x,y)) {
				StaticSprites.ctbSprites[i].setPosition(positions[i].x+deltaX,positions[i].y);
				selectedType = i;
			}
		}
	}
}
